package com.gitee.wsl.ext.number

/**
 * 分页彩虹算法<br></br>
 * 来自：[
 * https://github.com/iceroot/iceroot/blob/master/src/main/java/com/icexxx/util/IceUtil.java](https://github.com/iceroot/iceroot/blob/master/src/main/java/com/icexxx/util/IceUtil.java)<br></br>
 * 通过传入的信息，生成一个分页列表显示
 *
 * @param pageNo       当前页
 * @param totalPage    总页数
 * @param displayCount 每屏展示的页数
 * @return 分页条
 */
fun rainbow(pageNo: Int, totalPage: Int, displayCount: Int = 10): IntArray {
    // displayCount % 2
    val isEven = (displayCount and 1) == 0
    val left = displayCount shr 1
    var right = displayCount shr 1

    var length = displayCount
    if (isEven) {
        right++
    }
    if (totalPage < displayCount) {
        length = totalPage
    }
    val result = IntArray(length)
    if (totalPage >= displayCount) {
        if (pageNo <= left) {
            for (i in result.indices) {
                result[i] = i + 1
            }
        } else if (pageNo > totalPage - right) {
            for (i in result.indices) {
                result[i] = i + totalPage - displayCount + 1
            }
        } else {
            for (i in result.indices) {
                result[i] = i + pageNo - left + (if (isEven) 1 else 0)
            }
        }
    } else {
        for (i in result.indices) {
            result[i] = i + 1
        }
    }
    return result
}
